package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.models.auth.In;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void dishAdd(DishDTO dsdsto) {
        System.out.println("(😄)将DishDTO转成一个Dish，通过DishDTO"+dsdsto);
        Dish ds = new Dish();
        BeanUtils.copyProperties(dsdsto,ds);
        System.out.println("(😄)DishDTO转成了Dish"+ds);

        System.out.println("(😄)现在加入这个Dish"+ds);
        dishMapper.dishAdd(ds);
        System.out.println("(😄)Dish加入成功");

        Long dsId = ds.getId();
        System.out.println("(😄)这个Dish的id是"+dsId);
        System.out.println("(😄)你要天DishDTO中的Flavors,通过DisDTO"+dsdsto);
        List<DishFlavor> dsflaS = dsdsto.getFlavors();
        System.out.println("(😄)你得到了DishFlavors"+dsflaS);

        if(dsflaS!=null&& !dsflaS.isEmpty()){

            System.out.println("(😄)为所有的DishFlavors添加Id"+dsId);
            dsflaS.forEach((e)->{
                e.setDishId(dsId);
            });
            System.out.println("(😄)添加完后的DishFlavors"+dsflaS);
            System.out.println("(😄)现在将其加入sql"+dsflaS);
            dishFlavorMapper.dishFlavorAdd(dsflaS);
            System.out.println("(😄)DishFlavors加入完成"+dsflaS);


        }



    }

    @Override
    public PageResult dishPAge(DishPageQueryDTO dddQto) {
        PageHelper.startPage(dddQto.getPage(),dddQto.getPageSize());
        System.out.println("(😄)你要通过DishPageQueryDTO"+dddQto);
        System.out.println("(😄)得到Name,CategoryId,Status来查询！"+dddQto.getName()+","+dddQto.getCategoryId()+","+dddQto.getStatus());
        Page<DishVO> pgs = dishMapper.dishPageAll(dddQto.getName(), dddQto.getCategoryId(),dddQto.getStatus());

        long total  =pgs.getTotal();
        List<DishVO> records = pgs.getResult();
        System.out.println("(😄)你得到了total和List<DishVO>,"+total+","+records);
        return new PageResult(total,records);



    }

    @Override
    public void dishDelete(List<Long> ids) {
        for (int i = 0; i < ids.size(); i++) {
            System.out.println("(😄)现在删除id"+ids.get(i));
            System.out.println("(😄)先查看id"+ids.get(i));
            DishVO ddish = dishMapper.dishById(ids.get(i));
            System.out.println("(😄)"+ids.get(i)+"的dishVO是"+ddish);

            if(ddish.getStatus().equals(StatusConstant.ENABLE)){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
            List<Setmeal> kks = setmealMapper.SetmealByDishId(Math.toIntExact(ids.get(i)));
            if(kks.isEmpty()) {
                dishFlavorMapper.deleteByDishIds(ids);
                dishMapper.dishDelete(ids);
            }
            else
                throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);

        }

    }

    @Override
    public DishVO dishById(Long id) {
        DishVO kk = dishMapper.dishById(id);
        return kk;
    }

    @Override
    public void dishUpdate(DishDTO dishDTO) {

        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        System.out.println("(😄)你的dishDTO转成了dish！！"+dish);
        dishMapper.dishUpdate(dish);
        System.out.println("(😄)你的dish"+dish+"加入了数据库！！cool");


        List<DishFlavor> flavors = dishDTO.getFlavors();
        System.out.println("(😄)你的dishDTO转成一些了dishFlavor"+flavors);

        List<Long> kk = new ArrayList<>();
        kk.add(dish.getId());
        System.out.println("(😄)现在要删dish.getId"+kk);
        dishFlavorMapper.deleteByDishIds(kk);
        System.out.println("(😄)删掉啦");
        if(!flavors.isEmpty()){
            for (int i = 0; i < flavors.size(); i++) {
                flavors.get(i).setDishId(dish.getId());
            }
            System.out.println("(😄)现在添加flavors::"+flavors);
            dishFlavorMapper.dishFlavorAdd(flavors);
        }


    }

    @Override
    public void dishChangeStatus(Integer status, Long id) {
        DishVO dish = dishMapper.dishById(id);
        Dish kk = new Dish();
        BeanUtils.copyProperties(dish,kk);
        kk.setStatus(status);
        dishMapper.dishUpdate(kk);
    }

    @Override
    public List<DishVO> dishByCateId(Long categoryId) {
        return dishMapper.dishByCateId(categoryId);

    }


    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */

    /**
     * 条件查询菜品和口味
     * @param
     * @return
     */
    public List<DishVO> listWithFlavor(Long categoryId){

        List<DishVO> kks = dishMapper.UUdishByCateId(categoryId);
        System.out.println("(😄)查到了:"+kks);
        System.out.println("(😄)封装flavors");
        for (int i = 0; i < kks.size(); i++) {
            List<DishFlavor> ffs = dishFlavorMapper.getByDishID(kks.get(i).getId());
            kks.get(i).setFlavors(ffs);
        }
        System.out.println("(😄)封装完成："+kks);
        return kks;

    }


}
